{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eded5aaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import cross_val_score, cross_validate, train_test_split\n",
    "from sklearn.metrics import accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7375c6e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('./cancer_dataset.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b0eb45d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.transpose()\n",
    "df = df.rename(columns=df.iloc[0])\n",
    "df.drop(df.index[0], inplace=True)\n",
    "df.drop(df.columns[-1], axis=1, inplace = True)\n",
    "df.isnull().sum().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e924c36e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               1-Dec     1-Mar     1-Sep    10-Mar    10-Sep    11-Mar  \\\n",
      "GSM1534845  2.092083  2.396031   3.27166  1.842742  4.895042  1.399216   \n",
      "GSM1534979  1.904374   2.48228  3.095668  1.725725  4.667523  1.379209   \n",
      "GSM1535081  1.841461  2.521056  3.248161  1.792159  4.747594  1.534677   \n",
      "GSM1535215  1.748701  2.474526   3.63824  1.878483  4.245571  1.935998   \n",
      "GSM1534965  1.789912  2.183225  2.749508  1.679155  4.912579  1.909971   \n",
      "GSM1535109  1.781005  2.357657  3.281047  1.756426  5.296651  2.457373   \n",
      "GSM1535147  1.619952  3.051868  3.861602  1.852095  3.922228   1.89563   \n",
      "GSM1534911  2.001572  1.945153  3.147446  1.570722  4.268358  2.279671   \n",
      "GSM1535062  1.958045  2.610805  3.178805  2.112697  5.028324  1.821228   \n",
      "GSM1534897  2.020477  1.997634  2.517637  2.043524  4.264425  2.061268   \n",
      "\n",
      "              11-Sep    12-Sep    14-Sep    15-Sep  ...     ZWINT      ZXDA  \\\n",
      "GSM1534845  5.335519  2.075015  6.607469   4.75694  ...  3.765932  5.547119   \n",
      "GSM1534979  5.405575  2.045595   6.23285  4.386855  ...  3.325455  6.120825   \n",
      "GSM1535081  5.758607  1.925113  5.879682  4.697801  ...  2.862412  6.458178   \n",
      "GSM1535215  5.303933   2.11631  6.946667  4.149269  ...  3.192949   6.02149   \n",
      "GSM1534965  5.951741   1.99031  6.051371  4.347052  ...  4.055482  6.008439   \n",
      "GSM1535109  5.354646  2.142744  6.985758  3.959807  ...   3.04679   5.51594   \n",
      "GSM1535147  5.680144  1.829882  5.254084  4.270619  ...  3.146525  5.894778   \n",
      "GSM1534911  5.211291  1.650846  6.248355  3.849211  ...  3.376223  5.507367   \n",
      "GSM1535062  5.419292  2.151622  6.142779  4.604569  ...  4.194514  5.358317   \n",
      "GSM1534897  5.376529  2.086449  6.668828  4.673555  ...  3.864412  5.860088   \n",
      "\n",
      "                ZXDB      ZXDC    ZYG11A    ZYG11B       ZYX     ZZEF1  \\\n",
      "GSM1534845  4.142963  2.064982  2.010597  3.851269  5.132107  3.540349   \n",
      "GSM1534979  3.625694  2.180502  2.273599   3.76421  5.112372  3.618031   \n",
      "GSM1535081  4.166823  2.201831  2.376838  4.196698  5.265217  3.780313   \n",
      "GSM1535215  4.152484  2.077058  1.958354  3.864045  5.143145  3.763174   \n",
      "GSM1534965  4.013693  2.839946  1.909326  3.987161  4.614471   3.69695   \n",
      "GSM1535109  3.856446  2.328648  2.165748  3.730308  4.634914  3.431636   \n",
      "GSM1535147  4.072385  1.882809  1.653581  4.127917  5.330146  3.580968   \n",
      "GSM1534911  4.238721   2.10236   1.97665  3.359794  4.593016   3.51164   \n",
      "GSM1535062  4.129667  2.091937  2.077589  4.278028  5.474752  4.121525   \n",
      "GSM1534897  4.509037  2.333203   1.69869  3.880175  5.229218  3.774517   \n",
      "\n",
      "                ZZZ3 label  \n",
      "GSM1534845  4.056931     1  \n",
      "GSM1534979  4.366178     1  \n",
      "GSM1535081  4.441034     0  \n",
      "GSM1535215   4.01121     0  \n",
      "GSM1534965  4.372009     1  \n",
      "GSM1535109  4.660456     0  \n",
      "GSM1535147  4.271393     0  \n",
      "GSM1534911  3.843259     1  \n",
      "GSM1535062  4.029798     0  \n",
      "GSM1534897  3.714051     1  \n",
      "\n",
      "[10 rows x 20255 columns]\n"
     ]
    }
   ],
   "source": [
    "df['label'] = 0\n",
    "label=df['label'].copy()\n",
    "label[0:264] = 1\n",
    "df['label'] = label\n",
    "print(df.sample(10))\n",
    "#X = df.drop(['label'], axis=1)\n",
    "#y = df['label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "54d25999",
   "metadata": {},
   "outputs": [],
   "source": [
    "#dt_clf =DecisionTreeClassifier()\n",
    "#scores =cross_val_score(dt_clf, X, y, scoring='accuracy', cv=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "076b5de3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#print('교차 검증별 정확도:', np.round(scores, 4))\n",
    "#print('평균 검증 정확도:', np.round(np.mean(scores),4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c3579ae9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                A1BG      A1CF     A2LD1       A2M     A2ML1    A4GALT  \\\n",
      "GSM1534793  3.575803  1.815535  3.407354  6.848847   1.94937  3.389573   \n",
      "GSM1534794  3.602704  1.903611  3.400847  6.504326  2.367093  3.035315   \n",
      "GSM1534795  4.089806  1.998825   3.73377  4.904531   2.20855  3.328281   \n",
      "GSM1534796  3.929732  1.930019  3.668371  3.957686  2.845188  2.855448   \n",
      "GSM1534797  3.645441  1.917237   3.59429  5.312689  2.115304  2.795185   \n",
      "...              ...       ...       ...       ...       ...       ...   \n",
      "GSM1535212  4.033221   1.89258  3.210717  6.293321  2.590193  3.352657   \n",
      "GSM1535213  4.002834  1.854365  3.527233  7.501961  1.781323  2.958576   \n",
      "GSM1535214  4.011994  1.862413  3.647449  6.756797  2.356507   3.32983   \n",
      "GSM1535215  4.207677  2.136624  3.604942  7.088294  2.232433  3.150873   \n",
      "GSM1535216  3.739802  2.163003  3.413835  6.491046  2.471126  2.900397   \n",
      "\n",
      "               A4GNT      AAA1      AAAS      AACS  ...    ZWILCH     ZWINT  \\\n",
      "GSM1534793  1.935099  1.982839  4.669173    3.0678  ...  1.901821  3.395375   \n",
      "GSM1534794  1.941405  1.807694   4.24075  3.758705  ...  2.127016  3.265353   \n",
      "GSM1534795  1.923295  1.946061  4.597667   3.49088  ...  2.622994  3.559725   \n",
      "GSM1534796  1.855836  1.727771  4.024512   2.84985  ...  2.399191  3.847016   \n",
      "GSM1534797   1.73155  1.644886  4.463236  3.022631  ...  2.156741  3.386223   \n",
      "...              ...       ...       ...       ...  ...       ...       ...   \n",
      "GSM1535212  1.750994  1.920465  3.905713  3.123122  ...   1.83964  3.490971   \n",
      "GSM1535213  2.203659  1.937867  4.414416  3.153282  ...   2.92065  3.444262   \n",
      "GSM1535214  2.241225  1.973879  4.720018  3.289117  ...  1.867075  2.948814   \n",
      "GSM1535215  2.133609  1.741359  4.771533  3.078966  ...  2.056143  3.192949   \n",
      "GSM1535216   1.73502  2.449073  4.091353  3.840291  ...  2.097478  3.320438   \n",
      "\n",
      "                ZXDA      ZXDB      ZXDC    ZYG11A    ZYG11B       ZYX  \\\n",
      "GSM1534793   6.34686  3.659673  2.529919  1.782195  3.769252  5.465261   \n",
      "GSM1534794  5.657011  4.127937   2.18607  2.042729  3.924281   4.82124   \n",
      "GSM1534795  5.708799  4.387543  1.922107  1.901744  4.126258  4.993717   \n",
      "GSM1534796  6.144403   4.09672  2.144641  1.893372  3.586055  5.175353   \n",
      "GSM1534797   5.63687  3.211165   1.89334  2.320021  4.140209  4.653637   \n",
      "...              ...       ...       ...       ...       ...       ...   \n",
      "GSM1535212  6.546797  4.056769  1.897823  2.420359  3.510229  4.896166   \n",
      "GSM1535213  5.536547  3.975591  1.720398  1.882592  4.336957   4.57719   \n",
      "GSM1535214  5.766648  3.448547  2.208233  1.862815  3.932398  5.168857   \n",
      "GSM1535215   6.02149  4.152484  2.077058  1.958354  3.864045  5.143145   \n",
      "GSM1535216  5.477405  4.758099  2.127386  1.774495  3.890817  4.619554   \n",
      "\n",
      "               ZZEF1      ZZZ3  \n",
      "GSM1534793  3.541981   3.87061  \n",
      "GSM1534794  3.670514  4.405655  \n",
      "GSM1534795  3.913525  3.976327  \n",
      "GSM1534796  3.854349  4.095984  \n",
      "GSM1534797  3.509094  3.998673  \n",
      "...              ...       ...  \n",
      "GSM1535212  3.706078  3.624159  \n",
      "GSM1535213  3.062365  4.521975  \n",
      "GSM1535214  3.892305  3.697686  \n",
      "GSM1535215  3.763174   4.01121  \n",
      "GSM1535216  3.011923  4.355136  \n",
      "\n",
      "[424 rows x 20228 columns]\n",
      "GSM1534793    1\n",
      "GSM1534794    1\n",
      "GSM1534795    1\n",
      "GSM1534796    1\n",
      "GSM1534797    1\n",
      "             ..\n",
      "GSM1535212    0\n",
      "GSM1535213    0\n",
      "GSM1535214    0\n",
      "GSM1535215    0\n",
      "GSM1535216    0\n",
      "Name: label, Length: 424, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "df1 = df.drop(df.columns[:26], axis=1)\n",
    "df1.head()\n",
    "X = df1.drop(['label'], axis=1)\n",
    "y = df1['label']\n",
    "X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.2 )\n",
    "print(X)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "adf19994",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "예측 정확도: 0.7882\n"
     ]
    }
   ],
   "source": [
    "rt_clf =RandomForestClassifier(n_estimators = 100)\n",
    "rt_clf.fit(X_train,y_train)\n",
    "pred = rt_clf.predict(X_test)\n",
    "print('예측 정확도: {0:.4f}'.format(accuracy_score(y_test, pred)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "04d383de",
   "metadata": {},
   "outputs": [],
   "source": [
    "#print('교차 검증별 정확도:', np.round(scores, 4))\n",
    "#print('평균 검증 정확도:', np.round(np.mean(scores),4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "04526337",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.to_csv(\"./cancer_dataset_ver1.csv\", header =True, index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "24a0678d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A1BG</th>\n",
       "      <th>A1CF</th>\n",
       "      <th>A2LD1</th>\n",
       "      <th>A2M</th>\n",
       "      <th>A2ML1</th>\n",
       "      <th>A4GALT</th>\n",
       "      <th>A4GNT</th>\n",
       "      <th>AAA1</th>\n",
       "      <th>AAAS</th>\n",
       "      <th>AACS</th>\n",
       "      <th>...</th>\n",
       "      <th>ZWINT</th>\n",
       "      <th>ZXDA</th>\n",
       "      <th>ZXDB</th>\n",
       "      <th>ZXDC</th>\n",
       "      <th>ZYG11A</th>\n",
       "      <th>ZYG11B</th>\n",
       "      <th>ZYX</th>\n",
       "      <th>ZZEF1</th>\n",
       "      <th>ZZZ3</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>GSM1534793</th>\n",
       "      <td>3.575803</td>\n",
       "      <td>1.815535</td>\n",
       "      <td>3.407354</td>\n",
       "      <td>6.848847</td>\n",
       "      <td>1.94937</td>\n",
       "      <td>3.389573</td>\n",
       "      <td>1.935099</td>\n",
       "      <td>1.982839</td>\n",
       "      <td>4.669173</td>\n",
       "      <td>3.0678</td>\n",
       "      <td>...</td>\n",
       "      <td>3.395375</td>\n",
       "      <td>6.34686</td>\n",
       "      <td>3.659673</td>\n",
       "      <td>2.529919</td>\n",
       "      <td>1.782195</td>\n",
       "      <td>3.769252</td>\n",
       "      <td>5.465261</td>\n",
       "      <td>3.541981</td>\n",
       "      <td>3.87061</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GSM1534794</th>\n",
       "      <td>3.602704</td>\n",
       "      <td>1.903611</td>\n",
       "      <td>3.400847</td>\n",
       "      <td>6.504326</td>\n",
       "      <td>2.367093</td>\n",
       "      <td>3.035315</td>\n",
       "      <td>1.941405</td>\n",
       "      <td>1.807694</td>\n",
       "      <td>4.24075</td>\n",
       "      <td>3.758705</td>\n",
       "      <td>...</td>\n",
       "      <td>3.265353</td>\n",
       "      <td>5.657011</td>\n",
       "      <td>4.127937</td>\n",
       "      <td>2.18607</td>\n",
       "      <td>2.042729</td>\n",
       "      <td>3.924281</td>\n",
       "      <td>4.82124</td>\n",
       "      <td>3.670514</td>\n",
       "      <td>4.405655</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GSM1534795</th>\n",
       "      <td>4.089806</td>\n",
       "      <td>1.998825</td>\n",
       "      <td>3.73377</td>\n",
       "      <td>4.904531</td>\n",
       "      <td>2.20855</td>\n",
       "      <td>3.328281</td>\n",
       "      <td>1.923295</td>\n",
       "      <td>1.946061</td>\n",
       "      <td>4.597667</td>\n",
       "      <td>3.49088</td>\n",
       "      <td>...</td>\n",
       "      <td>3.559725</td>\n",
       "      <td>5.708799</td>\n",
       "      <td>4.387543</td>\n",
       "      <td>1.922107</td>\n",
       "      <td>1.901744</td>\n",
       "      <td>4.126258</td>\n",
       "      <td>4.993717</td>\n",
       "      <td>3.913525</td>\n",
       "      <td>3.976327</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GSM1534796</th>\n",
       "      <td>3.929732</td>\n",
       "      <td>1.930019</td>\n",
       "      <td>3.668371</td>\n",
       "      <td>3.957686</td>\n",
       "      <td>2.845188</td>\n",
       "      <td>2.855448</td>\n",
       "      <td>1.855836</td>\n",
       "      <td>1.727771</td>\n",
       "      <td>4.024512</td>\n",
       "      <td>2.84985</td>\n",
       "      <td>...</td>\n",
       "      <td>3.847016</td>\n",
       "      <td>6.144403</td>\n",
       "      <td>4.09672</td>\n",
       "      <td>2.144641</td>\n",
       "      <td>1.893372</td>\n",
       "      <td>3.586055</td>\n",
       "      <td>5.175353</td>\n",
       "      <td>3.854349</td>\n",
       "      <td>4.095984</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GSM1534797</th>\n",
       "      <td>3.645441</td>\n",
       "      <td>1.917237</td>\n",
       "      <td>3.59429</td>\n",
       "      <td>5.312689</td>\n",
       "      <td>2.115304</td>\n",
       "      <td>2.795185</td>\n",
       "      <td>1.73155</td>\n",
       "      <td>1.644886</td>\n",
       "      <td>4.463236</td>\n",
       "      <td>3.022631</td>\n",
       "      <td>...</td>\n",
       "      <td>3.386223</td>\n",
       "      <td>5.63687</td>\n",
       "      <td>3.211165</td>\n",
       "      <td>1.89334</td>\n",
       "      <td>2.320021</td>\n",
       "      <td>4.140209</td>\n",
       "      <td>4.653637</td>\n",
       "      <td>3.509094</td>\n",
       "      <td>3.998673</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 20229 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                A1BG      A1CF     A2LD1       A2M     A2ML1    A4GALT  \\\n",
       "GSM1534793  3.575803  1.815535  3.407354  6.848847   1.94937  3.389573   \n",
       "GSM1534794  3.602704  1.903611  3.400847  6.504326  2.367093  3.035315   \n",
       "GSM1534795  4.089806  1.998825   3.73377  4.904531   2.20855  3.328281   \n",
       "GSM1534796  3.929732  1.930019  3.668371  3.957686  2.845188  2.855448   \n",
       "GSM1534797  3.645441  1.917237   3.59429  5.312689  2.115304  2.795185   \n",
       "\n",
       "               A4GNT      AAA1      AAAS      AACS  ...     ZWINT      ZXDA  \\\n",
       "GSM1534793  1.935099  1.982839  4.669173    3.0678  ...  3.395375   6.34686   \n",
       "GSM1534794  1.941405  1.807694   4.24075  3.758705  ...  3.265353  5.657011   \n",
       "GSM1534795  1.923295  1.946061  4.597667   3.49088  ...  3.559725  5.708799   \n",
       "GSM1534796  1.855836  1.727771  4.024512   2.84985  ...  3.847016  6.144403   \n",
       "GSM1534797   1.73155  1.644886  4.463236  3.022631  ...  3.386223   5.63687   \n",
       "\n",
       "                ZXDB      ZXDC    ZYG11A    ZYG11B       ZYX     ZZEF1  \\\n",
       "GSM1534793  3.659673  2.529919  1.782195  3.769252  5.465261  3.541981   \n",
       "GSM1534794  4.127937   2.18607  2.042729  3.924281   4.82124  3.670514   \n",
       "GSM1534795  4.387543  1.922107  1.901744  4.126258  4.993717  3.913525   \n",
       "GSM1534796   4.09672  2.144641  1.893372  3.586055  5.175353  3.854349   \n",
       "GSM1534797  3.211165   1.89334  2.320021  4.140209  4.653637  3.509094   \n",
       "\n",
       "                ZZZ3 label  \n",
       "GSM1534793   3.87061     1  \n",
       "GSM1534794  4.405655     1  \n",
       "GSM1534795  3.976327     1  \n",
       "GSM1534796  4.095984     1  \n",
       "GSM1534797  3.998673     1  \n",
       "\n",
       "[5 rows x 20229 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a410b3d5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
